package com.qunar.train.mock.network;

import com.alibaba.dubbo.config.ReferenceConfig;
import com.qunar.redis.storage.Sedis;
import com.qunar.redis.storage.Sedis2;
import com.qunar.tc.core.info.api.ServiceNode;

import org.I0Itec.zkclient.IZkConnection;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkConnection;
import org.I0Itec.zkclient.exception.ZkInterruptedException;
import org.I0Itec.zkclient.serialize.ZkSerializer;
import org.apache.poi.ss.formula.functions.T;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

import mockit.Mock;
import mockit.MockUp;
import qunar.tc.qtracer.QTraceClient;
import qunar.tc.qtracer.concurrent.QTraceRunnable;
import qunar.tc.qtracer.impl.QTraceClientGetter;

/**
 * Created by hongzhe.yao on 2018/7/18. modify by xudong.tai on 2018/7/30: 注释掉无关打印
 */
public class MockNetWork {

    public MockNetWork() {
    }

    public static void mock() {
        QtraceMock();
        ConfigMock();
        redisMock();
        zkMock();
    }

    public static void QtraceMock() {
        new MockUp<QTraceClientGetter>() {
            @Mock
            public QTraceClient getClient() {
                return null;
            }

            @Mock
            public void $clinit() {

            }
        };
        new MockUp<QTraceRunnable>() {
            @Mock
            public void $init(Runnable runnable) {
            }

        };
    }

    public static void ConfigMock() {
        new MockUp<ServiceNode>() {
            @Mock
            public void $init(ReferenceConfig<T> ref) {
                //System.out.println("ServiceNode init is mock OK");
                return;
            }
        };
    }

    public static void redisMock() {
        new MockUp<Sedis>(Sedis.class) {
            @Mock
            public void $init(String namespace, String cipher, String zkAddress) {
                //System.out.println("Sedis is mock OK");
                return;
            }
        };
        new MockUp<Sedis2>(Sedis2.class) {
            @Mock
            public void $init(String namespace, String cipher, int socketTimeout, int poolCoreSize, int poolMaxSize, String zkAddr) {
                //System.out.println("Sedis2 is mock OK");
                return;
            }
        };
    }

    public static void zkMock() {
        new MockUp<ZooKeeper>() {
            @Mock
            public void $init(String connectString, int sessionTimeout, Watcher watcher)
                    throws IOException {
                //System.out.println("ZooKeeper init is mock OK");
                return;
            }
        };

        new MockUp<ZkConnection>() {
            @Mock
            public void connect(Watcher watcher) {
                //System.out.println("zkConnection is mock OK");
                return;
            }
        };

        new MockUp<org.I0Itec.zkclient.ZkClient>() {
            @Mock
            public void $init(String serverstring) {
            }

            @Mock
            public void $init(String zkServers, int connectionTimeout) {
            }

            @Mock
            public void $init(String zkServers, int sessionTimeout, int connectionTimeout) {
            }

            @Mock
            public void $init(String zkServers, int sessionTimeout, int connectionTimeout, ZkSerializer zkSerializer) {
            }

            @Mock
            public void $init(IZkConnection connection) {
            }

            @Mock
            public void $init(IZkConnection connection, int connectionTimeout) {
            }

            @Mock
            public void $init(IZkConnection zkConnection, int connectionTimeout, ZkSerializer zkSerializer) {
            }

            @Mock
            public void subscribeStateChanges(final IZkStateListener listener) {
            }

            @Mock
            public String create(final String path, Object data, final CreateMode mode) throws ZkInterruptedException, IllegalArgumentException, org.I0Itec.zkclient.exception.ZkException, RuntimeException {
                return "OK";
            }
        };
    }
}
